package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;
    /**
     * 新增套餐
     * @param checkGroupIds
     * @param setmeal
     */
    @Override
    public void add(Integer[] checkGroupIds, Setmeal setmeal) {

        //新增套餐
        setmealMapper.add(setmeal);

        //设置中间表关系
        setSetmealAndCheckGroup(checkGroupIds,setmeal.getId());
        //将保存到数据库中的图片也在redis的set集合中存储一份
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

    }

    /**
     * 设置套餐和检查组中间表关系
     * @param checkGroupIds
     * @param id
     */
    private void setSetmealAndCheckGroup(Integer[] checkGroupIds, Integer id) {

        if (checkGroupIds!=null && checkGroupIds.length>0){

            setmealMapper.setSetmealAndCheckGroup(checkGroupIds,id);

        }

    }

    /**
     * 查询 分页数据
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义页面大小和当前页的页码
        PageHelper.startPage(currentPage,pageSize);
        //条件查询所有套餐信息
        List<Setmeal> setmealList = setmealMapper.findByConditon(queryString);

        //将数据包装
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查询所有套餐信息
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        return setmealMapper.findAll();
    }

    /**
     * 通过id查询套餐、检查组、检查项数据
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        //通过套餐id查询套餐
        Setmeal setmeal = setmealMapper.findById(id);

        //通过套餐id可以查询t_setmeal_checkgroup中的checkgroup_id,在注入CheckGroupMapper,去查询checkGroup的数据
        //可能有多个检查组，需要setmeal.setChecgGroups(list)
        List<CheckGroup> checkGroups = checkGroupMapper.findBySetmealId(setmeal.getId());
        for (CheckGroup checkGroup : checkGroups) {
           List<CheckItem> checkItems = checkItemMapper.findByCheckGroupId(checkGroup.getId());
           checkGroup.setCheckItems(checkItems);
        }

        setmeal.setCheckGroups(checkGroups);

        //通过checkgroup_id查询t_checkgroup_checkitem中的checkitem_id,注入CheckItemMapper,去查询CheckItem
        //还将checkItem封装到checkgroup中。

        return setmeal;
    }

    /**
     * 查询预约套餐数据
     * @return
     */
    @Override
    public Map getSetmealReport() {

        List<Map> list= setmealMapper.getSetmealReport();

        //创建一个套餐名称的集合，用来存储所有套餐名称
        List<String> names = new ArrayList<>();

        for (Map map : list) {
            String name = (String) map.get("name");
            names.add(name);
        }

        //定义一个map，已固定的格式进行数据的封装，已方便前台js进行解析
        Map<String, List> map = new HashMap<>();

        map.put("setmealNames",names);
        map.put("setmealCounts",list);

        return map;
    }
}
